<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/9/15
 * Time: 10:03
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Contract extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu1.user_username'] = ['like',"%" . $keyword . "%"];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('contract')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.q_user_id','left')
                ->join('up_u_user uuu1','uuu1.user_id = a.j_user_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('contract')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.q_user_id','left')
                ->join('up_u_user uuu1','uuu1.user_id = a.j_user_id','left')
                ->where($map)
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu1.user_username as username')
                ->order('contract_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['contract_status']==1){
                    $vo['contract_name'] = '月结';
                }elseif ($vo['contract_status']==2){
                    $vo['contract_name'] = '次月结';
                }elseif ($vo['contract_status']==3){
                    $vo['contract_name'] = '次次月结';
                }elseif ($vo['contract_status']==4){
                    $vo['contract_name'] = '现结';
                }elseif ($vo['contract_status']==5){
                    $vo['contract_name'] = '到付';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $banner_thumb = input('banner_thumb');
            $contract_status = input('contract_status');
            $contract_date = input('contract_date');
            if(strlen($contract_date)<2){
                return json(['code' => 100, 'msg' => '请输入2位数字,例如01']);
            }
            if($contract_date>28){
                return json(['code' => 100, 'msg' => '日期不能超过28']);
            }
            if(!empty($banner_thumb)){
                $data['banner_thumb'] = $banner_thumb;
            }
            $data['contract_date'] = $contract_date;
            $data['contract_status'] = $contract_status;
            if($contract_status==4||$contract_status==5){
                $data['contract_date'] = '';
            }else{
                $data['contract_date'] = $contract_date;
            }
            $res = Db::table('up_contract')->where('contract_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $contract_id = input('param.contract_id');
        $res = Db::name('contract')->where('contract_id',$contract_id)->find();
        $this->assign('id',$contract_id);
        $this->assign('res',$res);
        return $this->fetch();

    }

}